﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using Styx;
using Styx.Helpers;
using Styx.Loaders;
using Styx.Patchables;
using Styx.Plugins;
using Styx.Common;
using Styx.CommonBot;
using Styx.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.Misc;
using Styx.WoWInternals.Misc.DBC;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWCache;
using Styx.WoWInternals.WoWObjects;

using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Media;

using HelpDesk;
using KickPlugin;
using KickProfile;
using KickQB;

namespace KUpdatePlugin
{
    public class KUpdatePlugin : HBPlugin
    {
        public bool hasBeenInitialized = false;
        public bool hasBeenInitialized2 = false;
        public bool hasBeenInitialized3 = false;
        public static bool hasBeenInitialized4 = false;
        public bool kickupdated = false;
        
        #region Overrides except pulse
        public override string Author { get { return "Liferose"; } }
        public override Version Version { get { return new Version(1, 0, 0); } }
        public override string Name { get { return "Kick's Profile Pack Updater"; } }
        public override bool WantButton { get { return true; } }
        public override string ButtonText { get { return "Help Desk Links"; } }
        public override void OnButtonPress()
        {
             abreJanela();
        }
        private KickPlugin_Updater UpdaterPlugin;
        private KickQB_Updater UpdaterQB;
        private KickProfile_Updater UpdaterProfile;

        public override void Initialize()
        {
            if (!hasBeenInitialized)
            {
                Logging.Write(Colors.Chartreuse, "Loaded Kick's Mega Profile Pack Updater v" + Version.ToString());
                Logging.Write(Colors.Chartreuse, "Checking for Updates. This may take a few minutes.");
                //Thread.Sleep(2000);
                hasBeenInitialized = true;
                try
                {
                    UpdaterPlugin = new KickPlugin_Updater("http://kickupdator.googlecode.com/svn/", "");
                    if (UpdaterPlugin.UpdateAvailable())
                    {
                        Logging.Write(Colors.Chartreuse, "[Kick] [Plugin Updater]: Update to Rev. " + UpdaterPlugin.GetNewestRev().ToString() + " is available! You are on Rev. " + UpdaterPlugin.CurrentRev.ToString());
                        Logging.Write(Colors.Chartreuse, "[Kick] [Plugin Updater]: Starting update process...");
                        if (UpdaterPlugin.Update())
                        {
                            Logging.Write(Colors.Red, "[Kick] [Plugin Updater]: is now up to date! Honorbuddy Restart Required!");
                            kickupdated = true;
                        }
                        else
                        {
                            Logging.Write(Colors.Red, "[Kick] [Plugin Updater]: Error trying to update. Please update manually!");
                        }
                    }
                    else
                    {
                        Logging.Write(Colors.Chartreuse, "[Kick] [Plugin Updater]: is at Rev. " + UpdaterPlugin.CurrentRev.ToString() + " and is up to date!");
                    }
                }
                catch (Exception ex)
                {
                    Logging.Write(Colors.Red, "Error: Unable to update plugin. See Log for Details!");
                    Logging.Write(LogLevel.Diagnostic, "[Kick] [Plugin Updater]: Exception " + ex.Message);
                }
                try
                {
                    UpdaterQB = new KickQB_Updater("https://kicks-scripts.googlecode.com/svn/trunk/Kick%27s%20Profiles/1-90%20Questing%20Profile%20Pack/Quest%20Behaviors/", "");
                    if (UpdaterQB.UpdateAvailable())
                    {
                        Logging.Write(Colors.Chartreuse, "[Kick] [Quest Behavior Updater]: Update to Rev. " + UpdaterQB.GetNewestRev().ToString() + " is available! You are on Rev. " + UpdaterQB.CurrentRev.ToString());
                        Logging.Write(Colors.Chartreuse, "[Kick] [Quest Behavior Updater]: Starting update process...");
                        if (UpdaterQB.Update())
                        {
                            Logging.Write(Colors.Red, "[Kick] [Quest Behavior Updater]: is now up to date! Honorbuddy Restart Required!");
                            kickupdated = true;
                        }
                        else
                        {
                            Logging.Write(Colors.Chartreuse, "[Kick] [Quest Behavior Updater]: Error trying to auto-update. Please update manually!");
                        }
                    }
                    else
                    {
                        Logging.Write(Colors.Chartreuse, "[Kick] [Quest Behavior Updater]: is at Rev. " + UpdaterQB.CurrentRev.ToString() + " and is up to date!");
                    }
                }
                catch (Exception ex)
                {
                    Logging.Write(Colors.Red, "Error: Unable to update quest behaviors. See Log for Details!");
                    Logging.Write(LogLevel.Diagnostic, "[Kick] [Quest Behavior Updater]: Exception " + ex.Message);
                }
                try
                {
                    UpdaterProfile = new KickProfile_Updater("https://kicks-scripts.googlecode.com/svn/trunk/Kick%27s%20Profiles/", "");
                    if (UpdaterProfile.UpdateAvailable())
                    {
                        Logging.Write(Colors.Chartreuse, "[Kick] [Profile Updater]: Update to Rev. " + UpdaterProfile.GetNewestRev().ToString() + " is available! You are on Rev. " + UpdaterProfile.CurrentRev.ToString());
                        Logging.Write(Colors.Chartreuse, "[Kick] [Profile Updater]: Starting update process...");
                        if (UpdaterProfile.Update())
                        {
                            Logging.Write(Colors.Chartreuse, "[Kick] [Profile Updater]: is now up to date!");
                        }
                        else
                        {
                            Logging.Write(Colors.Red, "[Kick] [Profile Updater]: Error trying to auto-update. Please update manually!");
                        }
                    }
                    else
                    {
                        Logging.Write(Colors.Chartreuse, "[Kick] [Profile Updater]: is at Rev. " + UpdaterProfile.CurrentRev.ToString() + " and is up to date!");
                    }
                }
                catch (Exception ex)
                {
                    Logging.Write(Colors.Red, "Error: Unable to update profiles. See Log for Details!");
                    Logging.Write(LogLevel.Diagnostic, "[Kick] [Profile Updater]: Exception " + ex.Message);
                }
                GlobalSettings.Instance.Load();
              }
            if (!hasBeenInitialized2)
            {
                hasBeenInitialized2 = true;
                return;
            }
            if (!hasBeenInitialized3)
            {
                hasBeenInitialized3 = true;
                return;
            }
            abreJanela();
        }

        public override void Dispose()
        {
            Logging.Write(Colors.Teal, "KickPlugin Disposed");
        }
        #endregion

        #region Privates/Publics
        private void abreJanela()
        {
            if (kickupdated)
            {
                MessageBox.Show("The Kick Updater Plugin or Quest Behaviors have been updated and a Honorbuddy restart is required!", "RESTART REQUIRED", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            var mainHelp_Desk = new Help_Desk();
            mainHelp_Desk.ShowDialog();
        }
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        #endregion
        
        #region Quests
        public List<WoWUnit> MobDocZapnozzle { get { return ObjectManager.GetObjectsOfType<WoWUnit>().Where(ret => (ret.Entry == 36608)).OrderBy(ret => ret.Distance).ToList(); } }

        #endregion

        #region Override Pulse
        public override void Pulse()
        {
            if (Me.Race == WoWRace.Goblin && Me.HasAura("Near Death!") && Me.ZoneId == 4720 && MobDocZapnozzle.Count > 0)
            {
                MobDocZapnozzle[0].Interact();
                Thread.Sleep(1000);
                Lua.DoString("RunMacroText('/click QuestFrameCompleteQuestButton')");
            }
        }
        #endregion
    }

}
